package com.team.hiring.talent.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.team.hiring.common.util.DBUtil;
import com.team.hiring.talent.dao.TalentDao;
import com.team.hiring.talent.entity.TalentEntity;

public class TalentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	TalentDao talentDao = new TalentDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


		this.doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, 
			HttpServletResponse response) throws ServletException, IOException {
		//解决request请求的乱码问题
		request.setCharacterEncoding("UTF-8");
		
		String command = request.getParameter("command");
		if("homepageUi".equals(command)){
			homepageUi(request,response);
		}else if("toLoginUi".equals(command)){
			toLoginUi(request,response);
		}else if("login".equals(command)){
			login(request,response);
		}else if("toRegisterUi".equals(command)){
			toRegisterUi(request,response);
		}else if("register".equals(command)){
			register(request,response);
		}else if("toChangePasswordPage".equals(command)){
			toChangePasswordPage(request,response);
		}else if("changePassword".equals(command)){
			changePassword(request,response);
	    }else if("returnHome".equals(command)){
			homepageUi(request,response);}
	    else if("toChangeInfo".equals(command)){
		    toChangeInfo(request,response);}
	    else if("changeInfo".equals(command)){
	    	changeInfo(request,response);
	    }else if("foundpassword".equals(command)){
	    	foundpassword(request,response);
	    }else if("selectCode".equals(command)){
	    	selectCode(request,response);
	    }
		else{
			System.out.println("请求非法！");
		}
	}

	

	




private void selectCode(HttpServletRequest request,
			HttpServletResponse response) {
	String valueCode = request.getParameter("valueCode");
	int result = talentDao.selectCode(valueCode);
	if (result == 0) {
		try {
			response.getWriter().print("yes");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}else{
		try {
			response.getWriter().print("no");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		
	}

private void changeInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	TalentEntity talentEntity = (TalentEntity) request.getSession().getAttribute("talentEntity");
	if(talentEntity!=null){
	int talentId = Integer.parseInt(request.getParameter("talentId"));
	String name = request.getParameter("name");
	String phone = request.getParameter("phone");
	String email = request.getParameter("email");
	int age = Integer.parseInt(request.getParameter("age"));
	int gender = Integer.parseInt(request.getParameter("gender"));
	String hobby = request.getParameter("hobby");
	TalentEntity talent = new TalentEntity();
	talent.setId(talentId);
	talent.setName(name);
	talent.setPhone(phone);
	talent.setEmail(email);
	talent.setAge(age);
	talent.setGender(gender);
	talent.setHobby(hobby);
	int row = talentDao.changeInfo(talent);
	
	if(row>0){
		request.getRequestDispatcher("TalentServlet?command=login&code="+talentEntity.getCode()+"&password="+talentEntity.getPassword()).forward(request, response);
	}
	}else{
		System.out.println("TalentEntity82错误");
	}
	}

private void toChangeInfo(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	request.getRequestDispatcher("WEB-INF/view/talent/talent-change-info.jsp").forward(request, response);
		
	}

private void toChangePasswordPage(HttpServletRequest request,
			HttpServletResponse response) {
	try {
		request.getRequestDispatcher("WEB-INF/view/talent/talent-update.jsp").forward(request, response);
	} catch (ServletException e) {
		
		e.printStackTrace();
	} catch (IOException e) {
	
		e.printStackTrace();
	}
		
	}

/**
 * 修改密码
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
	private void changePassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 1、获取参数ssesion
		int id = Integer.parseInt(request.getSession().getAttribute("talentId")
				.toString());
		String password = request.getSession().getAttribute("password")
				.toString();
		// page
		String oldpwd = request.getParameter("oldpwd");// 旧密码
		String newpwd = request.getParameter("newpwd");// 新密码
		String confirm = request.getParameter("confirm");// 确认新密码
		// 修改结果
		if (password.equals(oldpwd) && newpwd.equals(confirm)) {
			int row = talentDao.updatePassword(id, newpwd);
			if (row > 0) {
				// 注销
				request.getSession().invalidate();
				// 保存到request中，转发显示
				request.getRequestDispatcher(
						"/WEB-INF/view/talent/talent-login.jsp?result=fail")
						.forward(request, response);
			} else {
				request.getRequestDispatcher(
						"/WEB-INF/view/talent/talent-update.jsp?result=fail")
						.forward(request, response);
			}

		}
	}

	/**
	 * 
	 * 跳转到首页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void homepageUi(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.getRequestDispatcher("homepage.jsp").forward(request, response);
	}
	
	/**
	 * 
	 * 跳转到求职者登陆界面
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void toLoginUi(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			request.getRequestDispatcher("/WEB-INF/view/talent/talent-login.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
//		request.getRequestDispatcher("WEB-INF/view/talent/talent-login.jsp").forward(request, response);
	}

	/**
	 *求职者登录 
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		//1、获取参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		
		//2、调用业务逻辑
	    TalentEntity talentEntity =talentDao.login(code,password);
		//3、转向页面
		request.getSession().setAttribute("talentEntity", talentEntity);
		
		if (talentEntity.getCode() !=null) {
			request.getRequestDispatcher("ResumeServlet?command=selectResume").forward(request, response);
		} else {
			request.getRequestDispatcher("/WEB-INF/view/talent/talent-login.jsp?result=fail").forward(request, response);
		}
		
	}
	
	/**
	 * 跳转到注册页面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void toRegisterUi(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/view/talent/talent-register.jsp").forward(request, response);
	}
	
	/**
	 * 求职者注册
	 * 
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 */
	private void register(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		//1、获取参数
		String code = request.getParameter("code");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String phone = request.getParameter("phone");
		String email = request.getParameter("email");
		int age = Integer.parseInt(request.getParameter("age"));
		int  gender = Integer.parseInt(request.getParameter("gender"));
		String hobby = request.getParameter("hobby");
		//2、调用业务逻辑
		TalentEntity talent = new TalentEntity();
		talent.setCode(code);
		talent.setPassword(password);
		talent.setName(name);
		talent.setPhone(phone);
		talent.setEmail(email);
		talent.setAge(age);
		talent.setGender(gender);
		talent.setHobby(hobby);
		int row =talentDao.register(talent);
		//3、转向页面
		if(row != 0){
			request.getRequestDispatcher("WEB-INF/view/talent/talent-login.jsp").forward(request, response);
		}else{
			System.out.println("注册失败！");
		}
	}
	
	

/**
 * 发送邮件
 * @param request
 * @param response
 * @throws IOException 
 * @throws ServletException 
 */
private void foundpassword(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("WEB-INF/view/common/forget-password.jsp").forward(request, response);		
	}

}
